package com.sxj.corejava.code08_常用类.object;

/**
 * @author 石小俊
 */
public class Test01 {

    public static void main(String[] args) throws CloneNotSupportedException {
//        getClassTest();
//        equalsTest();
//        hashCodeTest();
//        toStringTest();
//        cloneTest();
        finalizeTest();
    }

    public static void getClassTest() {
        Person person = new Man();
        Person person2 = new WoMan();
        System.out.println(person);
        // getClass():用于返回当前对象的运行时类型
        // 对于person,返回值即为Man的class类型
        System.out.println(person.getClass());
        System.out.println(person2.getClass());

        if (person instanceof Man) {
            Man man = (Man) person;
            man.work();
        }

        // 每一个类也可以直接查看class属性,其返回的也是当前类的class类型
        // 语法:类名.class
        System.out.println(Person.class);
        System.out.println(Man.class);
        System.out.println(WoMan.class);

        // 我们可以通过当前对象的class类型来判断该对象是否可以转换成指定类型
        if (person2.getClass() == WoMan.class) {
            WoMan woMan = (WoMan) person2;
            woMan.shopping();
        }
    }

    public static void equalsTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        System.out.println(person == person2);  // false
        System.out.println(person.equals(person2)); // false
        String s1 = new String("admin");
        String s2 = new String("admin");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

    public static void hashCodeTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        Person person3 = new Person("admin", 20);
        System.out.println(person.equals(person2));
        System.out.println(person3.equals(person2));
        System.out.println(person.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person3.hashCode());
    }

    public static void toStringTest() {
        Person person = new Person("admin", 18);
        // 在我们直接打印对象的时候,本质上是调用了该对象的toString方法
        // 其返回值是由几部分组成
        // getClass().getName() + "@" + Integer.toHexString(hashCode());
        System.out.println(person);
        System.out.println(person.toString());
        System.out.println("name:" + person.getName() + ",age:" + person.getAge());
    }

    public static void cloneTest() throws CloneNotSupportedException {
        Person person = new Person("admin", 18);
//        Object obj = person.clone();
//        if(obj instanceof Person){
//
//        }

        Address address = new Address("江苏省", "南京市");
        person.setAddress(address);

        Person person2 = (Person) person.clone();
        System.out.println(person);
        System.out.println(person2);

        System.out.println("*********************修改地址*********************");
        person2.getAddress().setCity("苏州市");
        person2.setName("jack");
        // 此时发现两个对象全部改变了
        System.out.println(person);
        System.out.println(person2);

        System.out.println("*********************修改地址*********************");
        Address address2 = new Address("浙江省", "杭州市");
        person2.setAddress(address2);
        // 此时只改变了一个对象
        System.out.println(person);
        System.out.println(person2);
    }

    public static void finalizeTest() {
        Person person = new Person();
        Person person2 = person;
        Person person3 = person;
        person = null;
        person2 = null;
        person3 = null;
//        System.gc();
        Runtime.getRuntime().gc();
    }
}
